import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MutualFund extends Security {

	private int mutualFundId = 0;
	private int accountId;
	private int portfolioId;
	private String mutualFundName;
	private String mutualFundDesc;
	private boolean isActive = false;
	private double mutualFundPrice;
	private int stockQuantity = 0;
	private int availableMFQuantity = 0;
	private int mutualFundUnits = 0;
	private static Map<Stock, Integer> stockDetails;
	private Map<Integer, Integer> stockDetailsInMf;
	private ArrayList<AbstractOrder> orders = new ArrayList<AbstractOrder>();

	public int getMutualFundUnits() {
		return mutualFundUnits;
	}

	public void setMutualFundUnits(int mutualFundUnits) {
		this.mutualFundUnits = mutualFundUnits;
	}

	public int getAvailableMFQuantity() {
		return availableMFQuantity;
	}

	public void setAvailableMFQuantity(int availableMFQuantity) {
		this.availableMFQuantity = availableMFQuantity;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public int getAccountId() {
		return accountId;
	}

	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public int getPortfolioId() {
		return portfolioId;
	}

	public void setPortfolioId(int portfolioId) {
		this.portfolioId = portfolioId;
	}

	public MutualFund() {
		stockDetails = new HashMap<Stock, Integer>();
	}

	public Map<Integer, Integer> getStockDetailsInMf() {
		return stockDetailsInMf;
	}

	public void setStockDetailsInMf(Map<Integer, Integer> stockDetailsInMf) {
		this.stockDetailsInMf = stockDetailsInMf;
	}

	public static Map<Stock, Integer> getStockDetails() {
		return stockDetails;
	}

	public void setStockDetails(Map<Stock, Integer> stockDetails) {
		MutualFund.stockDetails = stockDetails;
	}

	public ArrayList<AbstractOrder> getOrders() {
		return orders;
	}

	public void setOrders(ArrayList<AbstractOrder> orders) {
		this.orders = orders;
	}

	public int getStockQuantity() {
		return stockQuantity;
	}

	public void setStockQuantity(int stockQuantity) {
		this.stockQuantity = stockQuantity;
	}

	public double getMutualFundPrice() {
		return mutualFundPrice;
	}

	public void setMutualFundPrice(double mutualFundPrice) {
		this.mutualFundPrice = mutualFundPrice;
	}

	public int getMutualFundId() {
		return mutualFundId;
	}

	public void setMutualFundId(int mutualFundId) {
		this.mutualFundId = mutualFundId;
	}

	public String getMutualFundName() {
		return mutualFundName;
	}

	public void setMutualFundName(String mutualFundName) {
		this.mutualFundName = mutualFundName;
	}

	public String getMutualFundDesc() {
		return mutualFundDesc;
	}

	public void setMutualFundDesc(String mutualFundDesc) {
		this.mutualFundDesc = mutualFundDesc;
	}

	public void addSecurity(int mutualFundId) {
		this.mutualFundId = mutualFundId;
		mutualFundName = "MutualFundName_" + mutualFundId;
		mutualFundDesc = "MutualFundDesc_" + mutualFundId;
		isActive = true;

		Stock stock;
		int stockId, quantity;
		double totalStockPrice = 0;
		int totalQuantity = 0;
		for (Map.Entry<Integer, Integer> stockDetail : getStockDetailsInMf()
				.entrySet()) {
			stockId = stockDetail.getKey();
			quantity = stockDetail.getValue();
			stock = StockRepository.getStockRepository().getStock(stockId);
			if (stock != null) {
				stockDetails.put(stock, quantity);
				totalStockPrice += (stock.getStockPrice() * quantity);
				totalQuantity += quantity;
			} else {
				System.out.println("Stock Id: " + stockId
						+ " does not exists! ");
			}
		}
		this.mutualFundPrice = totalStockPrice / ((double) totalQuantity);
		MutualFundRepository mutualFundRepository = MutualFundRepository
				.getMutualFundRepository();
		mutualFundRepository.addMutualFunds(this);
	}

	@Override
	public void listSecurity() {
		for (MutualFund mutualFund : MutualFundRepository
				.getMutualFundRepository().getMutualFund()) {
			System.out.println("\nMutual Id: " + mutualFund.getMutualFundId());
			System.out.println("Mutual Fund Name: "
					+ mutualFund.getMutualFundName());
			System.out.println("Mutual Fund Desc: "
					+ mutualFund.getMutualFundDesc());
			System.out.println("Mutual Fund Price: "
					+ mutualFund.getMutualFundPrice());

			System.out.println("\nStocks inside Mutual Funds: ");
			Stock stock;
			int quantity;
			for (Map.Entry<Stock, Integer> stockDetail : getStockDetails()
					.entrySet()) {
				stock = stockDetail.getKey();
				quantity = stockDetail.getValue();
				System.out.println("\nStock Id: " + stock.getStockId());
				System.out.println("Stock Name: " + stock.getStockName());
				System.out.println("Stock Desc: " + stock.getStockDesc());
				System.out.println("Current Stock Price: "
						+ stock.getStockPrice());
				System.out.println("Quantity: " + quantity);
			}
		}
	}

	public static void listMutualFunds(ArrayList<MutualFund> mutualFunds) {
		for (MutualFund mutualFund : mutualFunds) {
			System.out.println("\nMutual Id: " + mutualFund.getMutualFundId());
			System.out.println("Mutual Fund Name: "
					+ mutualFund.getMutualFundName());
			System.out.println("Mutual Fund Desc: "
					+ mutualFund.getMutualFundDesc());
			System.out.println("Mutual Fund Price: "
					+ mutualFund.getMutualFundPrice());

			System.out.println("\nStocks inside Mutual Funds: ");
			Stock stock;
			int quantity;
			for (Map.Entry<Stock, Integer> stockDetail : getStockDetails()
					.entrySet()) {
				stock = stockDetail.getKey();
				quantity = stockDetail.getValue();
				System.out.println("\nStock Id: " + stock.getStockId());
				System.out.println("Stock Name: " + stock.getStockName());
				System.out.println("Stock Desc: " + stock.getStockDesc());
				System.out.println("Current Stock Price: "
						+ stock.getStockPrice());
				System.out.println("Quantity: " + quantity);
			}
		}
	}
}
